package cn.tedu.loding.controller;

import cn.tedu.loding.pojo.dto.AttributeAddNewDTO;
import cn.tedu.loding.pojo.dto.AttributeUpdateDTO;
import cn.tedu.loding.pojo.vo.*;
import cn.tedu.loding.service.IAttributeService;
import cn.tedu.loding.service.IAttributeTemplateService;
import cn.tedu.loding.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api(tags = "02.属性管理")
@Slf4j
@RestController
@Validated
@RequestMapping("/attributes")
public class AttributeController {
    @Autowired
    IAttributeService attributeService;
    @Autowired
    IAttributeTemplateService attributeTemplateService;




    //localhost:9090/attributes/add-new
    @ApiOperation("添加属性")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult<String> addNew(@Valid AttributeAddNewDTO attributeAddNewDTO){
        log.debug("开始处理【添加属性】请求,参数为{}",attributeAddNewDTO);
        attributeService.addNew(attributeAddNewDTO);
        String message = "添加成功!";
        log.debug(message);
        return JsonResult.ok(message);
    }

    //localhost:9090/attributes/9527/delete
    @ApiOperation("删除属性")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id",value = "属性",required = true,type = "long")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<String> delete(@Range(min = 1,message = "尝试删除的属性ID") @PathVariable Long id){
        log.debug("开始处理【删除属性】请求,参数为{}",id);
        attributeService.delete(id);
        String message = "删除成功!";
        log.debug(message);
        return JsonResult.ok(message);
    }

    // http://localhost:9090/attributes/hello/update
    @ApiOperation("根据id修改属性")
    @ApiOperationSupport(order = 300)
    @PostMapping("/update")
    public JsonResult<String> update(@Valid AttributeUpdateDTO attributeUpdateDTO) {
        String message = "尝试修改id为【" + attributeUpdateDTO.getId() + "】的属性";
        log.debug(message);
        attributeService.update(attributeUpdateDTO);
        return JsonResult.ok("修改成功");
    }


    // http://localhost:9090/attribute
    @ApiOperation("根据id查询属性详情")
    @ApiOperationSupport(order = 410)
    @GetMapping("/{id:[0-9]+}/getStandardById")
    public JsonResult<AttributeStandardVO> getStandardById(@PathVariable Long id){
        String message = "尝试根据id为【"+ id +"】查询属性详情";
        log.debug(message);
        AttributeStandardVO attributeStandardVO = attributeService.getStandardById(id);
        return JsonResult.ok(attributeStandardVO);
    }

    //localhost:9090/attributes/product
    @ApiOperation("商品列表")
    @ApiOperationSupport(order = 420)
    @GetMapping("/product")
    public JsonResult<List<ProductListVO>> list(){
        log.debug("开始处理【商品列表】请求,无参数");
        List<ProductListVO> list = attributeTemplateService.list();
        return JsonResult.ok(list);
    }

    // http://localhost:9090/attributes/attributeListByProductId/ProductId=9925
    @ApiOperation("根据商品id查询属性")
    @ApiOperationSupport(order = 440)
    @GetMapping("/attributeListByProductId")
    public JsonResult<List<AttributeStandardVO>> attributes(Long productId){
        log.debug("开始处理【根据商品id查询属性】请求,参数为{}",productId);
       List<AttributeStandardVO>list =
                attributeTemplateService.AttributeList(productId);
        return JsonResult.ok(list);
    }

    // http://localhost:9090/attributes/attributeTemplateListByProductId/ProductId=9925
    @ApiOperation("根据商品id查询到的属性模板id")
    @ApiOperationSupport(order = 440)
    @GetMapping("/attributeTemplateListByProductId")
    public JsonResult<List<AttributeTemplateStandardVO>> attributeTemplates(Long productId){
        log.debug("开始处理【根据商品id查询到的属性模板】请求,参数为{}",productId);
        List<AttributeTemplateStandardVO>list =
                attributeTemplateService.Templates(productId);
        return JsonResult.ok(list);
    }

}
